package edu.feignServer.controller;

import edu.feignServer.feign.UserFeign;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JPhoebe on 2017/7/7.
 */

@RestController
public class FeignController {

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/group/{id}")
    public Object findUserById(@PathVariable(value = "id") long id){
//        LoggerFactory.getLogger(this.getClass()).info("Feign客户端");
//        return userFeign.findGroup(id);

        return this.restTemplate.getForObject("http://GroupService/group/"+id, Object.class);


    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping(value = "/test")
    public HashMap<String,String> test() {

        ServiceInstance serviceInstance = this.loadBalancerClient.choose("UserService");
        ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("GroupService");
        HashMap<String,String> map = new HashMap<>();
        map.put("UserService",serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());
        map.put("GroupService",serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":" + serviceInstance2.getPort());
        return map;
    }


}
